package by.grsu.pochebut.webpage.booking;


import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import by.grsu.pochebut.entity.Booking;
import by.grsu.pochebut.interf.InterfBooking;
import by.grsu.pochebut.webpage.MyPage;

@SuppressWarnings("serial")
public class ListBookingPage  extends MyPage {

	@SpringBean
	private InterfBooking daoBook;

	public ListBookingPage() {
		super();
		// retrieve all objects
		final List<Booking> bookings = daoBook.findAll();

		// add "list" component and provide list of items which should be
		// rendered
		add(new ListView<Booking>("bookings", bookings) {

			@Override
			protected void populateItem(final ListItem<Booking> item) {
				final Booking book = item.getModelObject();
				// add "labels" to display each property of the item
				item.add(new Label("client",String.valueOf(book.getClient())));
				item.add(new Label("car", String.valueOf(book.getCar())));
				item.add(new Label("bill", String.valueOf(book.getBill())));

				// add link which will forward to the "edit" page
				item.add(new Link<Booking>("editlink", new Model<Booking>(book)) {
					@Override
					public void onClick() {
						setResponsePage(new EditBookingPage(book));
					}
				});

				// add "link" which removes item from database and reload the
				// page
				item.add(new Link<Booking>("deletelink", new Model<Booking>(book)) {
					@Override
					public void onClick() {
						daoBook.delete(getModelObject().getId());
						setResponsePage(ListBookingPage.class);
					}
				});
			}

		});

		// redirect to the "edit" page using "empty" model to create new item as
		// result
		add(new Link<Booking>("createLink", new Model<Booking>(new Booking())) {
			@Override
			public void onClick() {
				setResponsePage(new EditBookingPage(getModelObject()));
			}
		});

	}
}
